// 公告相关API请求封装
const { request } = require('../utils/request');

// 发布公告
const createNotice = (data) => {
  return request('/api/notice/create', {
    method: 'POST',
    data: data
  });
};

// 获取公告列表
const getNotices = (params = {}) => {
  return request('/api/notice/list', {
    method: 'GET',
    data: params
  });
};

// 获取公告详情
const getNoticeDetail = (id, params = {}) => {
  const { userId, deviceType, userAgent, ipAddress } = params;
  let url = `/api/notice/detail/${id}`;
  const queryParams = [];
  
  console.log('API调用参数:', { id, userId, deviceType, userAgent, ipAddress });
  
  if (userId) queryParams.push(`user_id=${userId}`);
  if (deviceType) queryParams.push(`device_type=${encodeURIComponent(deviceType)}`);
  if (userAgent) queryParams.push(`user_agent=${encodeURIComponent(userAgent)}`);
  if (ipAddress) queryParams.push(`ip_address=${encodeURIComponent(ipAddress)}`);
  
  if (queryParams.length > 0) {
    url += '?' + queryParams.join('&');
  }
  
  console.log('最终请求URL:', url);
  
  return request(url, {
    method: 'GET'
  });
};

// 更新阅读时长
const updateReadDuration = (id, data) => {
  return request(`/api/notice/read-duration/${id}`, {
    method: 'PUT',
    data: data
  });
};

// 更新公告
const updateNotice = (id, data) => {
  return request(`/api/notice/update/${id}`, {
    method: 'PUT',
    data: data
  });
};

// 删除公告
const deleteNotice = (id) => {
  return request(`/api/notice/delete/${id}`, {
    method: 'DELETE'
  });
};

// 更新公告状态
const updateNoticeStatus = (id, status) => {
  return request(`/api/notice/status/${id}`, {
    method: 'PUT',
    data: { status }
  });
};

// 获取公告统计
const getNoticeStats = () => {
  return request('/api/notice/stats', {
    method: 'GET'
  });
};

// 搜索公告
const searchNotices = (params = {}) => {
  return request('/api/notice/search', {
    method: 'GET',
    data: params
  });
};

module.exports = {
  createNotice,
  getNotices,
  getNoticeDetail,
  updateNotice,
  deleteNotice,
  updateNoticeStatus,
  getNoticeStats,
  updateReadDuration,
  searchNotices
};
